import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Form Demo"),
      ),
      body: InputWidget(),
    ),
  ));
}

class InputWidget extends StatefulWidget {
  @override
  _InputWidget createState() => _InputWidget();
}

class _InputWidget extends State<InputWidget> {

  final TextEditingController _unameController = TextEditingController();
  final TextEditingController _passWordController = TextEditingController();

  @override
  Widget build(BuildContext context) {

    var column = Column(
      children: [
        TextField(
          autofocus: true,
          controller: _unameController,
          onChanged: (value) => print("onChanged-name: $value"),
          decoration: const InputDecoration(
            labelText: "用户名",
            hintText: "用户名或邮箱",
            prefixIcon: Icon(Icons.people),
          ),
        ),
        TextField(
          onChanged: (value) => print("onChanged-password: $value"),
          controller: _passWordController,
          decoration: const InputDecoration(
            labelText: "密码",
            hintText: "您的登录密码",
            prefixIcon: Icon(Icons.lock),
          ),
        ),
      ],
    );
    return column;
  }

  @override
  void initState() {
    // 从第三个字符开始监控
    // _unameController.selection = TextSelection(
    //   baseOffset: 2,
    //   extentOffset: _unameController.text.length
    // );
    _unameController.addListener(() { print("_unameController: ${_unameController.text}");});
    _passWordController.addListener(() {print("_passWordController: ${_passWordController.text}");});
  }
}
